package com.szs.controller;

import com.szs.pojo.Clazz;
import com.szs.pojo.ClazzQueryParam;
import com.szs.pojo.PageResult;
import com.szs.pojo.Result;
import com.szs.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;
    /*
    * 分页查询班级列表
    * */
    @GetMapping
    public Result clazzPage(ClazzQueryParam clazzQueryParam){
        log.info("分页查询:{}",clazzQueryParam);
        PageResult<Clazz> pageResult = clazzService.clazzPage(clazzQueryParam);
        return Result.success(pageResult);
    }
    /*
    * 根据id查询班级
    * */
    @GetMapping("{id}")
    public Result getClazzById(@PathVariable Integer id){
        log.info("查询id班级信息：{}",id);
        Clazz clazz = clazzService.getClassById(id);
        return Result.success(clazz);
    }
    /*
    * 查询所有班级
    * */
    @GetMapping("/list")
    public Result getClazzList(){
        log.info("查询所有员工信息");
        List<Clazz> clazzList = clazzService.findAll();
        return Result.success(clazzList);
    }

    /*
    * 新增班级信息 json 格式请求参数
    * */
    @PostMapping
    public Result save(@RequestBody Clazz clazz){
        log.info("新增班级信息：{}",clazz);
        clazzService.save(clazz);
        return Result.success();

    }

    /*
    * 修改班级信息
    * */
    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("修改id班级信息：{}",clazz);
        clazzService.update(clazz);
        return Result.success();
    }

    /*
    * 删除班级信息
    * */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        try {
            log.info("根据id删除班级信息：{}",id);
            clazzService.delete(id);
            return Result.success();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
